package com.lottery.im.module.service.presenter;

import android.app.Activity;
import android.util.Log;
import com.google.gson.Gson;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.RetryWithDelay;
import com.lottery.im.api.observer.BaseListObserver;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.result.ApiResult;
import com.lottery.im.api.result.entity.ServiceData;
import com.lottery.im.api.result.entity.UserInfo;
import com.lottery.im.api.result.entity.msg_bulletin.MessageCount;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.service.view.ServiceView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ServicePresenter extends BaseFragmentPresenter<ServiceView> {

  String token = MyApplication.getInstance().getToken();
  String userId = MyApplication.getInstance().getUserInfo().getUserId();
  public ServicePresenter(Activity activity,
      ServiceView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void loadServiceData() {
    ApiClient.getApiRetrofitInstance()
        .getServiceData()
        .throttleFirst(2, TimeUnit.SECONDS)
        //.retryWhen(new RetryWithDelay(100,10000))
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseListObserver<ServiceData.ReturnListBean>() {
          @Override public void onStart() {

          }

          @Override public void onSuccess(List<ServiceData.ReturnListBean> t) {
            iView.loadSuccess(t);
          }

          @Override public void onFail(String code, String msg) {

          }

          @Override public void onException(String msg) {

          }

          @Override public void onFinish() {

          }
        });
    }

    public void queryUserCommonInfo(){
      ApiClient.getApiRetrofitInstance()
          .queryUserCommonInfo(userId,token)
          .throttleFirst(2, TimeUnit.SECONDS)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .compose(provider.<ApiResult<UserInfo>>bindToLifecycle())
          .subscribe(new BaseObserver<UserInfo>() {
            @Override public void onStart() {

            }

            @Override public void onSuccess(UserInfo userCommonMapBean) {
              iView.queryUserCommonInfo(userCommonMapBean);
            }

            @Override public void onFail(String code, String msg) {

            }

            @Override public void onException(String msg) {

            }

            @Override public void onFinish() {

            }
          });
    }
}
